import sys
import time
from loguru import logger
from poium import config
from common.config import PathConfig


class Logger:

    def __init__(self, level: str = "DEBUG", colorlog: bool = True):
        self.logger = logger
        self._colorlog = colorlog
        self._console_format = "<green>{time:YYYY-MM-DD HH:mm:ss}</> {file} <level>| {level} | {message}</level>"
        self._level = level
        self.set_level(self._colorlog, self._level)

    def set_level(self, colorlog: bool = True, level: str = "DEBUG"):
        # 关闭控制台打印
        logger.remove()
        # 添加控制台输出的格式,sys.stdout为输出到屏幕;
        logger.add(sys.stderr, level=level, colorize=colorlog, backtrace=False, diagnose=False,
                   # format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "  # 颜色>时间
                   #        # "{process.name} | "  # 进程名
                   #        # "{thread.name} | "  # 进程名
                   #        "<cyan>{module}</cyan>.<cyan>{function}</cyan>"  # 模块名.方法名
                   #        ":<cyan>{line}</cyan> | "  # 行号
                   #        "<level>{level}</level>: "  # 等级
                   #        "<level>{message}</level>",  # 日志内容
                   format='{time:YYYY-MM-DD HH:mm:ss} - '  # 时间
                   # "{process.name} | "  # 进程名
                   # "{thread.name} | "  # 进程名
                          '{module}.{function}:{line} - {level} -{message}',  # 模块名.方法名:行号
                   )

        logger.add(PathConfig.log_path+"test_{time}.log", encoding='utf-8', level=level, enqueue=True, backtrace=True, diagnose=True,
                   format='{time:YYYY-MM-DD HH:mm:ss} - '  # 时间
                          # "{process.name} | "  # 进程名
                          # "{thread.name} | "  # 进程名
                          '{module}.{function}:{line} - {level} -{message}',  # 模块名.方法名:行号
                   )

    def trace(self, msg: str):
        if config.printLog is True:
            now = time.strftime("%Y-%m-%d %H:%M:%S")
            print(f"{now} | TRACE | {str(msg)}")
        return self.logger.trace(msg)

    def debug(self, msg: str):
        if config.printLog is True:
            now = time.strftime("%Y-%m-%d %H:%M:%S")
            print(f"{now} | DEBUG | {str(msg)}")
        return self.logger.debug(msg)

    def info(self, msg: str):
        if config.printLog is True:
            now = time.strftime("%Y-%m-%d %H:%M:%S")
            print(f"{now} | INFO | {str(msg)}")
        return self.logger.info(msg)

    def success(self, msg: str):
        if config.printLog is True:
            now = time.strftime("%Y-%m-%d %H:%M:%S")
            print(f"{now} | SUCCESS | {str(msg)}")
        return self.logger.success(msg)

    def warning(self, msg: str):
        if config.printLog is True:
            now = time.strftime("%Y-%m-%d %H:%M:%S")
            print(f"{now} | WARNING | {str(msg)}")
        return self.logger.warning(msg)

    def error(self, msg: str):
        if config.printLog is True:
            now = time.strftime("%Y-%m-%d %H:%M:%S")
            print(f"{now} | ERROR | {str(msg)}")
        return self.logger.error(msg)

    def critical(self, msg: str):
        if config.printLog is True:
            now = time.strftime("%Y-%m-%d %H:%M:%S")
            print(f"{now} | CRITICAL | {str(msg)}")
        return self.logger.critical(msg)

    def printf(self, msg: str):
        return self.logger.success(msg)


# log level: TRACE < DEBUG < INFO < SUCCESS < WARNING < ERROR
logging = Logger(level="TRACE")